#include <iostream>

using namespace std;
//成员属性设置为私有
//优点1:将所有成员属性设置为私有，可以自己控制读写权限
//优点2:对于写权限，我们可以检测数据的有效性

class Person {
private:
    string name;//姓名
    int age;//年龄
    string wife;//伴侣
public:
    string getName() {//可读
        return name;
    }

    void setName(string name) {//可写
        this->name = name;
    }

    void setAge(int age) {//可写
        if (age < 0 || age > 100) {
            cout << "年龄数据设置有误,已更改为默认值" << endl;
            this->age = 18;
        } else {
            this->age = age;
        }

    }

    int getAge() {//可读
        return age;
    }


    void setWife(string wife) {//只写不可读
        this->wife = wife;
    }
};

int main() {
    Person person;
    person.setName("张三");
    person.setAge(-10);
    person.setWife("小红");
    cout << "姓名:" << person.getName() << ",年龄为:" << person.getAge() << endl;
    return 0;
}